//
//  FootmarkViewController.m
//  Funny
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 King. All rights reserved.
//

#import "FootmarkViewController.h"

@interface FootmarkViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign) NSInteger flag;

@end

@implementation FootmarkViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.flag = 1; // 刷新计数
    [self handleData];
    [self.view addSubview:self.tableView];
    
    // Do any additional setup after loading the view.
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
}

- (NSMutableArray *)dataArr
{
    if (!_dataArr) {
        self.dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellAccessoryNone;
        [_tableView registerClass:[FootMarkTableViewCell class] forCellReuseIdentifier:@"reuse"];
        [_tableView registerClass:[FootTableViewCell class] forCellReuseIdentifier:@"use"];
        _tableView.header = [MyRefreshHeaderGif headerWithRefreshingTarget:self refreshingAction:@selector(handleDataPullDown)]; // 下拉刷新
        _tableView.footer = [MyRefreshFooterGif footerWithRefreshingTarget:self refreshingAction:@selector(handleDataPullUp)]; // 上啦加载
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FootMarkModel *model = _dataArr[indexPath.row];
    if (model.pics.count != 0) {
        FootMarkTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
        cell.model = model;
        cell.selectionStyle = UITableViewCellAccessoryNone; // cell 不可选择
        return cell;
    }
    else{
        FootTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"use"];
        cell.model = model;
        cell.selectionStyle = UITableViewCellAccessoryNone;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FootMarkModel *model = _dataArr[indexPath.row];
    if (model.pics.count != 0) {
        return ScreenWidth / 4 * 3 + 40 + 100;
    }
    else{
        FootTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"use"];
        cell.model = model;
        cell.selectionStyle = UITableViewCellAccessoryNone;
        return 140;
    }
}

// 下拉刷新

- (void)handleDataPullDown
{
    self.flag = 1;
    self.dataArr = [NSMutableArray array];
    [self handleData];
    [self.tableView.header endRefreshing];
}

// 上拉加载

- (void)handleDataPullUp
{
    self.flag += 11;
    [self handleData];
    [self.tableView.footer endRefreshing];
}

- (void)handleData
{
    NSString *url = [NSString stringWithFormat:@"http://api.mxtrip.cn/api/ugc/getGeoFootprint?cityid=kWX3ingF&city=%@&from=%ld&num=10",self.name,(long)self.flag];
    [NetHandler getDataWithURL:url completion:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        for (NSDictionary *dataDic in [dic objectForKey:@"data"]) {
            FootMarkModel *model = [FootMarkModel modelWithDic:dataDic];
            [self.dataArr addObject:model];
        }
        [self.tableView reloadData];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
